<?php
	/**
	* Note: Use together with smarty plugin: smarty_function_formhelper_list
	*/
	if (!defined('EXPONENT')) exit('');
	
	define("FORMHELPER_GRID_TYPE_DATAGRID",0);
	define("FORMHELPER_GRID_TYPE_TREEGRID",1);
	
	/**
	* Form Helper
	* @author trungdt
	* @version 1.0
	* @since Apr, 2012
	*/
	class formhelper {
		// Data variables
		private $_url_action = "";
		private $_columns = array();
		private $_frozencolumns = array();
		private $_data= array();
		private $_target_module="";
		// 
		private $_template;
		private $_title = "";
		private $_js=array();
		private $_css=array();
		
		// grid configuration
		private $_grid_id = "formhelper";
		private $_grid_js_callback_initready = "";
		private $_grid_js_preinit = "";
		private $_grid_type = FORMHELPER_GRID_TYPE_TREEGRID;
		private $_grid_column_type = 0; // = 0: Load in html ; =1: load in js
		
		private $_grid_options = array("height"=>500, "width"=>"auto");
		
		private $_grid_toolbar_items=array();
		private $_grid_toolbar_id="fh_toolbar";
		private $_grid_toolbar_style="";
		private $_grid_toolbar_html="";
		
		private $_grid_contextmenu_items=array();
		private $_grid_contextmenu_id="fh_context";
		private $_grid_contextmenu_html="";
		private $_grid_contextmenu_style="min-width: 120px;";
		/**
		* Form Helper Construction
		* 
		* @param mixed $module : module name
		* @param mixed $title : page title . You can use {language_key} for load from language instead of manual load
		* @param mixed $view : module view
		* @param mixed $loc : module location
		*/
		function formhelper($module, $title, $view ="", $loc="")
		{
			$this->_target_module =  $module;
			$this->_title = $title;
			$this->_template = new template("formhelpermodule","Default", $loc);
			if ($view != "")
				// set langdir, for automatically language file import
				$this->_template->langdir = "modules/{$module}/{$view}/";
		}
		
		/**
		* Set based url data stream. URL Stream data must be return data in json format
		* 
		* @param mixed $url
		*/
		function setURLDataStream($url)
		{
			$this->_url_action = $url;
		}
		
		/**
		* how to generate column
		* 
		* @param mixed $type = 0: generate in HTML; =1: generate in js
		*/
		function setColumnType($type)
		{
			$this->_grid_column_type = $type;
		}
		
		/**
		* Set data for gird
		* 
		* @param mixed $data Can be object or array or object
		*/
		function setData($data)
		{
			if (!is_array($data))
				$data = array($data);
			$this->_data = $data;
		}
		
		/**
		* Add new button into toolbar
		* 
		* @param mixed $title 
		* @param mixed $icon
		* @param mixed $onClick
		*/
		function addToolbarButtons($title, $icon, $onClick){
			$button = null;
			$button->title=$title;
			$button->icon=$icon;
			$button->onclick=$onClick;
			
			//
			$this->_grid_toolbar_items[]=$button;
		}
		
		/**
		* Insert HTML of Toolbar
		* 
		* @param mixed $html
		*/
		function addToolbarHTML($html){
			$this->_grid_toolbar_html = $html;
		}
		
		/**
		* Add style for toolbar
		* 
		* @param mixed $style
		*/
		function addToolbarStyle($style)
		{
			$this->_grid_toolbar_style=$style;
		}
		
		/**
		* Set toolbar ID
		* 
		* @param mixed $id
		*/
		function addToolbarId($id)
		{
		  $this->_grid_toolbar_id = $id;  
		}
		
		/**
		* Insert HTML of Context menu
		* 
		* @param mixed $html
		*/
		function addContextMenuHTML($html){
			$this->_grid_contextmenu_html= $html;
		}
		
		/**
		* Add context item
		* 
		* @param mixed $title
		* @param mixed $icon
		* @param mixed $onClick
		*/
		function addContextItem($title, $icon, $onClick){
			$button = null;
			$button->title=$title;
			$button->icon=$icon;
			$button->onclick=$onClick;
			
			//
			$this->_grid_contextmenu_items[]=$button;
		}
		
		function setGridOptions_ByArray($options)
		{
			if (!is_array($options))
				$options = array($options);
				
			foreach ($options as $k=>$v)
			{
				if ($v == null)
					unset($this->_grid_options[$k]);
				else
					$this->_grid_options[$k] = $v;
			}
		}
		
		/**
		* Set Grid options. If value == null, then unset $key element
		* 
		* @param mixed $key Can be array or a value
		* @param mixed $value can be array or a value
		*/
		function setGridOptions($key, $value){
			if (!is_array($key))
				$key=array($key);
			if (!is_array($value))
				$value=array($value);
			
			for ($i = 0; $i < count($key); $i++)
			{
				$k=$key[$i];
				$v=$value[$i];
				
				if ($v == null)
					unset($this->_grid_options[$k]);
				else
					$this->_grid_options[$k] = $v;
			}
		}
		
		/**
		* Add columns
		* 
		* @param mixed $options
		* @param mixed $frozen = true: Add to FrozenColumn
		*/
		function addColumn($options, $frozen=false)
		{
			if (!is_array($options))
				$options = array($options);
			// convert to object
			$options=(object)$options;
			if ($frozen)
				$this->_frozencolumns[]=$options;
			else
				$this->_columns[]=$options;
		}
		
		/**
		* Add JS for manage form
		* 
		* @param mixed $full_url_to_js
		*/
		function addJS($full_url_to_js)
		{
			$this->_js[] = $full_url_to_js;
		}
		
		function addCss($css)
		{
			$this->_css[] = $css;
		}
		
		/**
		* Set Grid type
		* 
		* @param mixed $type FORMHELPER_GRID_TYPE_TREEGRID or FORMHELPER_GRID_TYPE_DATAGRID
		*/
		function setGridType($type)
		{
			$this->_grid_type = $type;
		}
		
		function setGridID($id)
		{
			$this->_grid_id = $id;
		}
		
		/**
		* Set preinit function name
		* 
		* @param mixed $js_function_name
		*/
		function setPreInitJSCall($js_function_name){
			$this->_grid_js_preinit = $js_function_name;
		}
		
		/**
		* Set javascript function name that formhelper will call back when init successfull
		* 
		* @param mixed $callback_js_function_name
		*/
		function setInitCallback($callback_js_function_name)
		{
			$this->_grid_js_callback_initready = $callback_js_function_name;
		}
		function register_permissions($perms, $locs)
		{
			$this->_template->register_permissions($perms,$locs);
		}
		
		/**
		* Valid array value before pass to smarty to avoid js error
		* 
		* @param mixed $array
		* @param bool $valid_object: should valid all properties in each object item 
		*/
		private function _validArray($array)
		{
			return $array;
			foreach ($array as $k=>$v)
				if (is_object($v))
				{
					foreach (get_object_vars($v) as $k1=>$v1)
						if (!is_numeric($v1))
						{
							// format string
							$v1="'".str_replace("\"","\\\"",$v1)."'";
							$v->$k1 = $v1;
						}
					$array[$k]=$v;
				}
				else if (!is_numeric($v))
				{
					// format string
					$v="'".str_replace("\"","\\\"",$v)."'";
					$array[$k]=$v;
				}
			return $array;
		}
		
		private function _beforeRender()
		{
			// insert url by action link
			$this->setGridOptions("url",$this->_url_action);
			
			// data
			$this->_template->assign("_data",$this->_data);
			//
			$this->_template->assign("_title",$this->_title);
			$this->_template->assign("_grid_id",$this->_grid_id);
			$this->_template->assign("_grid_js_callback_initready",$this->_grid_js_callback_initready);
			$this->_template->assign("_grid_js_preinit",$this->_grid_js_preinit);
			$this->_template->assign("_grid_type",$this->_grid_type);
			$this->_template->assign("_grid_option", $this->_validArray((object)$this->_grid_options));
			$this->_template->assign("_action_url",$this->_url_action);
			$this->_template->assign("_target_module",$this->_target_module);
			$this->_template->assign("_js_files",$this->_js);
			$this->_template->assign("_css_files",$this->_css);
			
			$this->_template->assign("_columns_type",$this->_grid_column_type);
			$this->_template->assign("_columns",$this->_validArray($this->_columns));
			$this->_template->assign("_fozencolumns",$this->_validArray($this->_frozencolumns));
			
			
			// toolbar
			$this->_template->assign("_toolbar_items",$this->_grid_toolbar_items);
			$this->_template->assign("_toolbar_style",$this->_grid_toolbar_style);
			$this->_template->assign("_toolbar_id",$this->_grid_toolbar_id);
			$this->_template->assign("_toolbar_html",$this->_grid_toolbar_html);
			
			// context
			$this->_template->assign("_context_items",$this->_grid_contextmenu_items);
			$this->_template->assign("_context_style",$this->_grid_contextmenu_style);
			$this->_template->assign("_context_id",$this->_grid_contextmenu_id);
			$this->_template->assign("_context_html",$this->_grid_contextmenu_html);
		}
		
		/**
		* Render this form into HTML
		* 
		*/
		function render()
		{
			$this->_beforeRender();
			return $this->_template->render();
		}
		
		/**
		* Output to HTML
		* 
		*/
		function output()
		{
			$this->_beforeRender();
			$this->_template->output();
		}
	}

?>
